﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Windows.Media;
using WP_Customer_ShopMob.Model;

namespace WP_Customer_ShopMob.Views
{
    public partial class LogPage : PhoneApplicationPage
    {
        IRepository repo = Data.GetModel; 
        public LogPage()
        {
            InitializeComponent(); 
            btn_valide.IsEnabled = false;
        }

        private void btn_valider(object sender, RoutedEventArgs e)
        {
                repo.Connexion(txt_login.Text, txt_password.Password, delegate(string resu, Exception error){
                    if (error == null)
                    {
                        if (resu == "1")
                        {
                            //LogViewModel lvm = new LogViewModel(txt_login.Text, txt_password.Text, ckb_remember.IsChecked.Value);
                            NavigationService.Navigate(new Uri("/Views/MainPage.xaml?id=" + txt_login.Text, UriKind.Relative));
                            if (ckb_remember.IsChecked.Value)
                            {
                                IsolatedStorageSettings.ApplicationSettings["login"] = txt_login.Text;
                                IsolatedStorageSettings.ApplicationSettings["datetime"] = DateTime.Now;
                                IsolatedStorageSettings.ApplicationSettings.Save();
                            }
                        }
                        else
                        {
                            txt_login.Text = "";
                            txt_password.Password = "";
                            txt_login.BorderBrush = new SolidColorBrush(Colors.Red);
                            txt_password.BorderBrush = new SolidColorBrush(Colors.Red);
                            isModelValid();
                        }
                    }
                    else {
                        txt_login.Text = "";
                        txt_password.Password = "";
                        txt_login.BorderBrush = new SolidColorBrush(Colors.Red);
                        txt_password.BorderBrush = new SolidColorBrush(Colors.Red);
                        //TODO prendre en compte les erreurs
                    }
                });
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            if (e.OriginalSource == txt_login )
            {
                if (String.IsNullOrEmpty((e.OriginalSource as TextBox).Text))
                {
                    txt_login.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                    btn_valide.IsEnabled = false;
                }
                else
                {
                    txt_login.Background = new SolidColorBrush(Color.FromArgb(75, 255, 255, 255));
                }
            }
            if (e.OriginalSource == txt_password)
            {
                if (String.IsNullOrEmpty((e.OriginalSource as PasswordBox).Password))
                {
                    txt_password.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                    btn_valide.IsEnabled = false;
                }
                else
                {
                    txt_password.Background = new SolidColorBrush(Color.FromArgb(75, 255, 255, 255));
                }
            }
            isModelValid();
        }

        private void isModelValid()
        {
            if (!String.IsNullOrEmpty(txt_login.Text) && !String.IsNullOrEmpty(txt_password.Password))
            {
                btn_valide.IsEnabled = true;
            }
            else
            {
                btn_valide.IsEnabled = false;
            }
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                if (e.NavigationMode == NavigationMode.Back)
                {
                    throw new Exception();
                }
                if (IsolatedStorageSettings.ApplicationSettings.Contains("login"))
                {
                    string login = IsolatedStorageSettings.ApplicationSettings["login"].ToString();
                    DateTime dt = Convert.ToDateTime(IsolatedStorageSettings.ApplicationSettings.Single(x => x.Key == "datetime").Value);
                    TimeSpan t = DateTime.Now - dt;
                    if (t.TotalMinutes < 10)
                    {
                        NavigationService.Navigate(new Uri("/Views/MainPage.xaml?id=" + login, UriKind.Relative));
                    }
                }
            }
            catch (Exception) 
            {
                Helper.IsolatedStorage.SaveData();
            }
            
        }

        private void PhoneApplicationPage_OrientationChanged_1(object sender, OrientationChangedEventArgs e)
        {
            if (e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
            {
                this.ContentPanel.Width = 750;
                this.ContentPanel.Height = 400;
            }
            else if (e.Orientation == PageOrientation.PortraitDown || e.Orientation == PageOrientation.PortraitUp)
            {
                this.ContentPanel.Width = 400;
                this.ContentPanel.Height = 800;
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            try
            {
                if (e.NavigationMode == NavigationMode.Back)
                {
                    throw new Exception();
                }
            }
            catch (Exception) 
            {
                Helper.IsolatedStorage.SaveData();
            }
        }

        private void btn_inscrire(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/InscriptionPage.xaml", UriKind.Relative));
        }
    }
}